#########################################################################
#
# Top-level Makefile.
#
#########################################################################

.PHONY: all debug clean distclean nuke

working_dir := ${shell pwd}
INSTALLDIR   = ${working_dir}

# Determine the platform we are running on
# (purely to define out-of-source directory names)
#
include ${working_dir}/make/detectplatform.mk

# Define DEBUG and or RELEASE builds
#
ifdef DEBUG
    variant += .debug
endif

# Define platform-dependend directories
#
top_build_dir := build
build_dir     := ${top_build_dir}/${platform}${variant}
top_dist_dir  := dist
dist_dir      := ${top_dist_dir}/${platform}${variant}

# Build options
#
ifdef DEBUG
MY_CMAKE_FLAGS += -DCMAKE_BUILD_TYPE:STRING=Debug
endif

#########################################################################

all: dist

# 'make debug'
#
debug:
	${MAKE} DEBUG=1 --no-print-directory

# 'make cmakesetup' constructs the build directory and runs 'cmake' there,
# generating makefiles to build the project.  For speed, it only does this when
# ${build_dir}/Makefile doesn't already exist, in which case we rely on the
# cmake generated makefiles to regenerate themselves when necessary.
#
cmakesetup:
	@ (if [ ! -e ${build_dir}/Makefile ] ; then \
		cmake -E make_directory ${build_dir} ; \
		cd ${build_dir} ; \
		cmake -DCMAKE_INSTALL_PREFIX=${INSTALLDIR}/${dist_dir} \
			${MY_CMAKE_FLAGS} \
			 -DPYTHON=${PYTHON_VERSION} \
             -DBOOST_ROOT=${BOOST_ROOT} \
			../../ ; \
	 fi)

# 'make cmake' does a basic build (after first setting it up)
#
cmake: cmakesetup
	( cd ${build_dir} ; ${MAKE} ${MY_MAKE_FLAGS} )

# 'make cmakeinstall' builds everthing and installs it in 'dist'.
#
cmakeinstall: cmake
	( cd ${build_dir} ; ${MAKE} ${MY_MAKE_FLAGS} install )

# 'make dist' is just a synonym for 'make cmakeinstall'
#
dist : cmakeinstall

# 'make test' does a full build and then runs all tests
#
test: cmake
	( cd ${build_dir} ; ${MAKE} ${MY_MAKE_FLAGS} test )

# 'make clean' clears out the build directory
#
clean:
	cmake -E remove_directory ${build_dir}

# 'make distclean' clears out both build and dist directories for this platform
#
distclean: clean
	cmake -E remove_directory ${dist_dir}

# 'make nuke' blows away the build and dist areas for all platforms
#
nuke:
	cmake -E remove_directory ${top_build_dir}
	cmake -E remove_directory ${top_dist_dir}

#########################################################################

# Pretty help
#
help:
	@echo "Targets:"
	@echo "  make              Build optimized binaries and libraries in ${dist_dir},"
	@echo "                        temporary build files in ${build_dir}"
	@echo "  make debug        Build unoptimized with symbols in ${dist_dir}.debug,"
	@echo "                        temporary build files in ${build_dir}.debug"
	@echo "  make clean        Remove the temporary files in ${build_dir}"
	@echo "  make distclean    Remove both ${build_dir} AND ${dist_dir}"
	@echo "  make nuke         Remove ALL of build and dist (not just ${platform})"
	@echo "  make test         Run all tests"
	@echo ""
	@echo "Helpful modifiers:"
	@echo "  make PYTHON_VERSION=2.6       Specify the Python version."
	@echo "  make BOOST_ROOT=/some/path    Specify the location of your BOOST installation"
	@echo ""
       
